import nltk
import pickle
from threading import Thread
from threading import Lock

# Class that provides an interface to maintain the features.
# It also allows for serialization of the models so that subsequent
# runs are faster.
class FeatureManager:
  def __init__(self):
    self.tuples = []
    self.stemmer = nltk.PorterStemmer()
    self.generators = []

  def num_tuples(self):
    return self.tuples.size()

  def get_tuples(self):
    return self.tuples

  def register_feature_generator(self, generator):
    self.generators.append(generator)

  def add_tuple_from_text(self, text, label, n=3):
    all_generator_tuple = {}
    for generator in self.generators:
      a_tuple = generator.generate(text)
      all_generator_tuple.update(a_tuple)

    # Add the tuple to the tuple_set and associate the passed label
    self.tuples.append((all_generator_tuple, label))

  # Serialize the feature tuples.
  def store_features(self, filename):
    pickle.dump(self.tuples, open(filename, 'w'))

  # Reset the internal tuples dictionary.
  def reset(self):
    self.tuples = []
